package com.youlai.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.system.mapper.SysPeriodMapper;
import com.youlai.system.model.entity.SysPeriod;
import com.youlai.system.model.query.PeriodPageQuery;
import com.youlai.system.model.vo.PeriodPageVO;
import com.youlai.system.service.SysPeriodService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class SysPeriodServiceImpl extends ServiceImpl<SysPeriodMapper, SysPeriod> implements SysPeriodService {
    @Override
    public List<SysPeriod> getPeriodList(Long userId, Long courseId) {
        LambdaQueryWrapper<SysPeriod> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysPeriod::getStudentId, userId).eq(SysPeriod::getCourseId, courseId).orderByDesc(SysPeriod::getPeriod);
        return list(queryWrapper);
    }

    @Override
    public IPage<PeriodPageVO> getPeriodPage(PeriodPageQuery queryParams) {
        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        Page<PeriodPageVO> page = new Page<>(pageNum, pageSize);
        // 查询数据
        Page<PeriodPageVO> userPage = this.baseMapper.getPeriodPage(page, queryParams);
        // 实体转换
        return userPage;
    }

    @Override
    public boolean clearPeriodBy(Long studentId, Long courseId) {
        LambdaQueryWrapper<SysPeriod> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysPeriod::getStudentId, studentId).eq(SysPeriod::getCourseId, courseId);
        return remove(queryWrapper);
    }

    @Override
    public boolean clearPeriodByCourseId(Long courseId) {
        LambdaQueryWrapper<SysPeriod> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysPeriod::getCourseId, courseId);
        return remove(queryWrapper);
    }
}
